package com.ebelter.seneo.ui.views.birthday;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.seneo.R;


import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class BirthdayView extends FrameLayout {

    private static final String TAG = "BirthdayView";

    private DateView dvMonth;
    private DateView dvDay;
    private DateView dvYear;

    private int curYear;
    private int curMonth;
    private int curDay;

    private int mYear = 1938;
    private int mMonth = 1;
    private int mDay = 1;

    private static int MIN_YEAR = 1938;
    private static final int MIN_MONTH = 1;
    private static final int MIN_DAY = 1;

    private static final int MAX_MONTH = 12;
    private String[] monthTexts = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

    public BirthdayView(@NonNull Context context) {
        super(context);


        initViews(context);
    }




    public BirthdayView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initViews(context);
    }

    public BirthdayView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initViews(context);
    }

    private void initViews(Context context) {
        LayoutInflater lf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mRootView = lf.inflate(R.layout.view_birthday, null);

        dvMonth = mRootView.findViewById(R.id.dv_date_1);
        dvDay = mRootView.findViewById(R.id.dv_date_2);
        dvYear = mRootView.findViewById(R.id.dv_date_3);

        //监听日期变化，限制day天数显示
        dvMonth.setOnValueChangedListener(new DateView.OnValueChangedListener() {
            @Override
            public void onValueChanged(int value) {
                mMonth = value;
                LogUtils.i(TAG, "---dvMonth------onValueChanged------value = " + value);
                limitDate(mYear, mMonth);
            }
        });

        dvDay.setOnValueChangedListener(new DateView.OnValueChangedListener() {
            @Override
            public void onValueChanged(int value) {
                mDay = value;
                LogUtils.i(TAG, "---dvDay------onValueChanged------value = " + value);
            }
        });

        dvYear.setOnValueChangedListener(new DateView.OnValueChangedListener() {
            @Override
            public void onValueChanged(int value) {
                mYear = value;
                LogUtils.i(TAG, "---dvYear------onValueChanged------value = " + value);
                limitDate(mYear, mMonth);
            }
        });

        addView(mRootView);

        initDateView();

    }

    private void initDateView() {

        Calendar curr = Calendar.getInstance();
        MIN_YEAR=curr.get(Calendar.YEAR)-80;



       curYear = Calendar.getInstance().get(Calendar.YEAR)-18;
        curMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
        curDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);

        Calendar calendar = new GregorianCalendar(curYear, curMonth - 1, 1);
        int countDaysOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        LogUtils.i(TAG, "----initDateView-----curYear = " + curYear + ", curMonth = " + curMonth + ", curDay = " + curDay);
        dvYear.initData(MIN_YEAR, curYear);


        monthTexts=getResources().getStringArray(R.array.months);

        //月份按字符创显示
        dvMonth.setShowTexts(monthTexts);
        dvMonth.initData(1, MAX_MONTH);

        dvDay.initData(1, countDaysOfMonth);


        //选中日期
//        setDate(mYear, mMonth, mDay);
    }

    /**
     * 设置日期， 供外部调用，包含日期检测
     */
    public void setCurrentDate(int year, int month, int day) {
        //设置日期的检测
        if (year > curYear || year < MIN_YEAR) {
            year = MIN_YEAR;
        }
        if (year == curYear && (month > curMonth || month < MIN_MONTH)) {
            month = MIN_MONTH;
        } else if (month > MAX_MONTH || month < MIN_MONTH) {
            month = MIN_MONTH;
        }
        Calendar calendar = new GregorianCalendar(year, month - 1, 1);
        int countDaysOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        if (day < MIN_DAY || day > countDaysOfMonth) {
            day = MIN_DAY;
        }
        //作上限设置
        limitDate(year, month);

        this.mYear = year;
        this.mMonth = month;
        this.mDay = day;

        setDate(year, month, day);


    }


    /**
     * 上限设置
     */
    private void limitDate(int year, int month) {

        //限制月份上下限
        int maxMonth = MAX_MONTH;

//        if (year == curYear) {//今年，当前月为最大月份
//            maxMonth = curMonth;
//            if (month >= curMonth) {
//                month = maxMonth;
//            }
//        }
        LogUtils.i(TAG, "-----limitDate-----year = " + year + ",curYear = " + curYear + ", month = " + month + ", maxMonth = " + maxMonth);
        dvMonth.initData(1, maxMonth);
        //切换至当前月份
        dvMonth.setValue(month);

        //限制日上下限
        Calendar calendar = new GregorianCalendar(year, month - 1, 1);
        int countDaysOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        dvDay.initData(1, countDaysOfMonth);

        int day = mDay;
        if (day > countDaysOfMonth) {
            day = countDaysOfMonth;
        }
        dvDay.setValue(day);
    }

    /**
     * 设置日期，仅供内部调用，不检测日期
     */
    private void setDate(int year, int month, int day) {
        LogUtils.i(TAG, "----setDate----year = " + year + ", month = " + month + ", day = " + day);
        dvYear.setValue(year);
        dvMonth.setValue(month);
        dvDay.setValue(day);
    }

    public Birthday getBirthday() {
        Birthday mBirthday = new Birthday();
        mBirthday.setYear(mYear);
        mBirthday.setMonth(mMonth);
        mBirthday.setDay(mDay);
        LogUtils.i(TAG, "-----getBirthday---- mBirthday = " + mBirthday);
        return mBirthday;
    }
}
